import type { AxiosError } from "axios";
import { sendMail } from "./mail/mail";
import {
  joinLottery,
  unJoinLotteryIds,
  unReceiveLottery,
} from "./mohe/lottery";
import { formatError } from "./utils/format";

try {
  console.log("查询是否有未参与的抽奖");
  let ids = await unJoinLotteryIds();
  ids.forEach(async (id: string) => {
    console.log(`参与抽奖: ${id}`);
    await joinLottery(parseInt(id, 10));
  });

  console.log("查询是否有未领取奖品");
  let list = await unReceiveLottery();
  list.forEach((item) => {
    sendMail("魔盒未领取奖励", `请领取奖品: ${item.name}`);
    console.log(`请领取奖品: ${item.name}`);
  });
} catch (e) {
  let msg = formatError(e as AxiosError);
  sendMail("魔盒抽奖异常", msg);
  console.log(msg);
}
